//
//  MWConfigManager.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/11/1.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWConfigManager.h"

@implementation MWConfigManager

+ (instancetype)manager {
    static MWConfigManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[MWConfigManager alloc] init];
        AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
        [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusUnknown:
                    break;
                case AFNetworkReachabilityStatusNotReachable:
                    manager.isNetError = YES;
                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    manager.isNetError = NO;
                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    manager.isNetError = NO;
                    break;
                default:
                    break;
            }
        }];
        [mgr startMonitoring];
    });
    return manager;
}

- (NSString *)objectForKey:(NSString *)key defaultValue:(NSString *)defaultValue {
    if (!_localizableDic || ![_localizableDic objectForKey:key] || [[_localizableDic objectForKey:key] length] == 0) {
        return defaultValue;
    }
    
    return [_localizableDic objectForKey:key];
}

- (void)saveConfig {
    NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];
    [userDetaults setObject:_localizableDic forKey:@"MWLocalizableDic"];
    [userDetaults synchronize];
}

- (void)loadConfig {
    NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];
    _localizableDic = [userDetaults objectForKey:@"MWLocalizableDic"];
    _productType = [userDetaults objectForKey:@"MWProductType"];
    if (!_productType) {
        _productType = @"1";
    }
    
    _language = [userDetaults objectForKey:@"MWLanguage"];
    if (!_language) {
        _language = @"1";
    }
}

- (void)setProductType:(NSString *)productType {
    _productType = productType;
    NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];
    [userDetaults setObject:_productType forKey:@"MWProductType"];
    [userDetaults synchronize];
}

- (void)setLanguage:(NSString *)language {
    _language = language;
    NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];
    [userDetaults setObject:_language forKey:@"MWLanguage"];
    [userDetaults synchronize];
}



@end
